
package com.hlkj.pay.controller.openapi.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.app.order.ISettleOrderAppService;
import com.hlkj.pay.app.openapi.IOpenApiAppService;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiMerchantAccessQueryReq;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiMerchantAccessSettleQueryReq;
import com.hlkj.pay.vo.openapi.merchant.req.access.OpenApiOrderSettleSubmitReq;
import com.hlkj.pay.vo.openapi.merchant.resp.access.OpenApiMerchantAccessBalanceResp;
import com.hlkj.pay.vo.openapi.merchant.resp.access.OpenApiOrderSettleResp;
import com.hlkj.pay.vo.order.OpenApiResult;
import com.hlkj.pay.vo.order.req.OrderCommonParamReq;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/10/09 11:25
 */
@Tag(name = "商户结算管理")
@RestController
@RequestMapping("/openapi/settle")
@RequiredArgsConstructor
@Slf4j
public class OpenApiOrderSetterController {

    private final IOpenApiAppService openApiAppService;

    private final ISettleOrderAppService merchantAccessAmountAppService;

    @Operation(summary = "账户余额查询")
    @PostMapping("/balance")
    OpenApiResult<OpenApiMerchantAccessBalanceResp> accessBalance(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiMerchantAccessQueryReq accessQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiMerchantAccessQueryReq.class);
            CommonResult<OpenApiMerchantAccessBalanceResp> stringOpenApiResult = merchantAccessAmountAppService.accessBalance(accessQueryReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "提交结算订单")
    @PostMapping("/create")
    OpenApiResult<OpenApiOrderSettleResp> accessSettle(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiOrderSettleSubmitReq accessQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiOrderSettleSubmitReq.class);
            CommonResult<OpenApiOrderSettleResp> stringOpenApiResult = merchantAccessAmountAppService.accessSettle(accessQueryReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

    @Operation(summary = "结算订单查询")
    @PostMapping("/query")
    OpenApiResult<OpenApiOrderSettleResp> accessSettleQuery(@RequestBody OrderCommonParamReq orderCommonParamReq) {
        try {
            OpenApiMerchantAccessSettleQueryReq accessQueryReq = JsonUtils.parseObject(orderCommonParamReq.getBizContent(), OpenApiMerchantAccessSettleQueryReq.class);
            CommonResult<OpenApiOrderSettleResp> stringOpenApiResult = merchantAccessAmountAppService.accessSettleQuery(accessQueryReq);
            return openApiAppService.sign(stringOpenApiResult);
        }
        catch (Exception e) {
            e.printStackTrace();
            return OpenApiResult.error(CommonResultCode.PARAMETER_ERROR.getCode(), e.getMessage());
        }
    }

}
